package com.wl.eb.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wl.eb.module.EbProduct;
import com.wl.eb.service.ProductService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 用一句话描述
 * @Auther: 新梦想*陈超
 * @Date: 2021/12/6 08:40
 */
@Controller//证
@RequestMapping("/product")//工号
public class ProductController {
    @Resource
    private ProductService productService;

    @RequestMapping("/detail")
    public String detail(Model model,int productId) throws Exception{
        //1.调用service根据pruductId查询该商品的详情
        EbProduct productDetail = productService.detail(productId);
        if(productDetail==null){
            throw new  RuntimeException("查询的商品不存在！！");
        }
        //2.将详情数据绑定到Model中
        model.addAttribute("detail",productDetail);
        //3.返回view
        return "product-view";//templates/product-view.html
    }
    @RequestMapping("/list")
    public String productListRelativedCategory(Model model,
                                               Integer categoryId,
                                               @RequestParam(value = "page",defaultValue = "1") int page){
        System.out.println("=========进入productListRelativedCategory..分类Id为:"+categoryId+",页数为:"+page);
        Page productPage = productService.list(page,categoryId);
        model.addAttribute("productPage",productPage);
        return "product-list";
    }
    /**
     * 根据商品id将商品放入购物车(购物车表，session作为购物车)
     * @param id
     * @return
     */
    @GetMapping("/add/car")
    public String addCar(int id, HttpServletRequest request){
        System.out.println("进入ProductController的addCar方法，id："+id);
        //1.调用service将商品添加到购物车
        productService.addCar(id,request);
        //2.绑定数据到model
        //3.返回view
        return "shopping";//templates/shopping.html
    }
    @GetMapping("/shop/result")
    public String shopResult(HttpServletRequest request){
        //清空购物车
        request.getSession().removeAttribute("car");
        return "shopping-result";//templates/shopping-result.html
    }
}
